package com.googlecode.axolotl.jpa;

import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;

import org.codehaus.groovy.grails.commons.GrailsApplication;

import com.googlecode.axolotl.Database;
import com.googlecode.axolotl.jdbc.GrailsDomainSqlFactory;
import com.googlecode.axolotl.jdbc.SpringJdbcDatabase;


public class AxolotlPersistenceProvider implements PersistenceProvider {
	
	private Database database;
	private GrailsApplication application;

	
	public void setDatabase(Database database) {
		this.database = database;
	}
	
	
	public void setGrailsApplication(GrailsApplication application) {
		this.application = application;
	}
	
	
	public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map arg1) {
		return new AxolotlEntityManagerFactory(database, application);
	}
	

	public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo pui, Map arg1) {
		GrailsDomainSqlFactory sqlFactory = new GrailsDomainSqlFactory();
		sqlFactory.setGrailsApplication(application);
		
		SpringJdbcDatabase database = new SpringJdbcDatabase();
		database.setDataSource( pui.getNonJtaDataSource() );
		database.setSqlFactory( sqlFactory );
		
		return new AxolotlEntityManagerFactory(database, application);
	}
}